app.controller('VehicleInsuranceModalCtrl', function ($scope, $filter, $modalInstance, toaster, param) {

	$scope.submitting = false;
	$scope.timeRange = {};
	$scope.param = param;
	if ($scope.param.data == null) {
		$scope.param.data = {};
	}

	if ($scope.param.type == "update") {
		var end = $filter("date")($scope.param.data.period, "yyyy-MM-dd");
		$scope.timeRange.endTime = end;
	}

	$scope.ok = function () {
		if ($scope.myForm.$valid) {
			if ($scope.param.type == "insert") {
				if ($scope.timeRange.startTime) {
					var starTime = $filter("date")($scope.timeRange.startTime, "yyyy-MM-dd");
					$scope.param.data.period = starTime;
				} else {
					toaster.pop('warning', '提示', '请选择有效期！');
					return;
				}
				if ($scope.number == undefined) {
					toaster.pop('warning', '提示', '请填写单号！');
					return;
				}
				$scope.param.data.number = $scope.number;
				$scope.param.data.remark = $scope.remark;
				$modalInstance.close($scope.param);
			}
			if ($scope.param.type == "update") {
				if ($scope.timeRange.endTime) {
					var endTime = $filter("date")($scope.timeRange.endTime, "yyyy-MM-dd");
					$scope.param.data.period = endTime;
				} else {
					toaster.pop('warning', '提示', '请选择有效期！');
					return;
				}
				if ($scope.param.data.number == undefined) {
					toaster.pop('warning', '提示', '请填写单号！');
					return;
				}
				if ($scope.param.data.remark == null) {
					$scope.param.data.remark = " ";
				}
				$modalInstance.close($scope.param);
			}
		}
	};

	$scope.cancel = function () {
		$modalInstance.dismiss('cancel');
		$scope.submitting = false;
	};
});